﻿using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.UI.WebControls;
using Foothill.Common;
using Foothill.Common.Logging;
using Foothill.IoC;
using Foothill.Service.Session;
using System;

namespace Foothill.Web
{
    public class FoothillBasePage : System.Web.UI.Page
    {
        // clean up session variables
        protected void CleanupDataInSession()
        {
            ISessionService _sessionService = IocContainer.GetInstance<ISessionService>();
            _sessionService.ClearSessionVariable(SessionNames.CompleteSearchResultList);
            _sessionService.ClearSessionVariable(SessionNames.DisplaySearchResultList);
        }

        public void Redirect(string pageName)
        {
            if (!string.IsNullOrEmpty(pageName))
            {
                try
                {
                    Response.Redirect(pageName);
                }
                catch(HttpException exception)
                {
                    Log.Error(FoothillModule.View, string.Format("Error in redirect to page {0}", pageName), exception);
                    throw;
                }
            }
        }

        public void PopulateDropDownList(ListControl ddl, IDictionary<int, string> dict)
        {
            ddl.Items.Clear();
            
            foreach (KeyValuePair<int, string> item in dict)
            {
                ListItem newItem = new ListItem(item.Value, item.Key.ToString());
                ddl.Items.Add(newItem);
            }
        }

        public bool IsDropDownListContainValue(ListControl ddl, string value)
        {
            foreach (ListItem item in ddl.Items)
            {
                if (item.Value.Equals(value))
                {
                    return true;
                }
            }

            return false;
        }

        // set the selected value only if the dropdownlist contains it
        public void SafelySetDropDownSelectedValue(ListControl ddl, string value)
        {
            if (IsDropDownListContainValue(ddl, value))
            {
                ddl.SelectedValue = value;
            }
        }

        public void AddItemsToDropDownList(ListControl ddl, IDictionary<int, string> dict)
        {
            foreach (KeyValuePair<int, string> item in dict)
            {
                ListItem newItem = new ListItem(item.Value, item.Key.ToString());
                ddl.Items.Add(newItem);
            }
        }

        public void SafelyAssignDropdownSelectedValue(DropDownList ddl, int value)
        {
            if (ddl.Items.FindByValue(value.ToString()) != null)
            {
                ddl.SelectedValue = value.ToString();
            }
        }

        protected int ConvertToInt(object obj)
        {
            if (obj == null)
            {
                return 0;
            }

            int result = 0;
            try
            {
                result = (int)obj;
            }
            catch (Exception)
            {
            }

            return result;
        }
    }
}